﻿using B.S.DataBase.MyProject.Domain.DiagnosisAndTreatmentManagement;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.DoctorCommand;
using MedicalPCTerminal.Read.API.Dtos;
using SqlSugar;
using System.Security.Cryptography;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.DoctorCommandHandler
{
    public class PatientInforQueryCommandHandler : IRequestHandler<PatientInforQueryCommand, ApiPaging<PatientInforDtos>>
    {

        /// <summary>
        /// sqlsugar
        /// </summary>

        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sugar"></param>
        public PatientInforQueryCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 获取患者信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiPaging<PatientInforDtos>> Handle(PatientInforQueryCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<PatientInforDtos> result= new ApiPaging<PatientInforDtos>();
            var list=sugar.db.Queryable<BloodPressureTest,CollectionManagement>((a,b)=>new object[]
            {
                JoinType.Left,a.UserId==b.UserId
            }).Where(a=>a.UserId==request.UserId).Select<PatientInforDtos>((a,b)=>new PatientInforDtos
            {
                BId=a.BId,
                UserId=a.UserId,
                UserName=a.UserName,
                Part=a.Part,
                OneSystolicPressure=a.OneSystolicPressure,
                OneDiastolicPressure=a.OneDiastolicPressure,
                TwoSystolicPressure=a.TwoSystolicPressure,
                TwoDiastolicPressure=a.TwoDiastolicPressure,
                ThreeSystolicPressure=a.ThreeSystolicPressure,
                ThreeDiastolicPressure=a.ThreeDiastolicPressure,
                AveSystolicPressure=a.AveSystolicPressure,
                AveDiastolicPressure=a.AveDiastolicPressure,
                CreateTime=a.CreateTime,
                IsDeleted=a.IsDeleted,
                CId=b.CId,
                CollCode=b.CollCode,
                CollName=b.CollName,
                Sex=b.Sex,
                IdentityCard=b.IdentityCard,
                Height=b.Height,
                Weight=b.Weight,
                BIM=b.BIM,
                FastingBloodGlucose=b.FastingBloodGlucose,
                PostprandialBloodGlucose=b.PostprandialBloodGlucose,
                Cholesterol=b.Cholesterol,
                Triglyceride=b.Triglyceride,
                HDLCholesterol=b.HDLCholesterol,
                LDLCholesterol=b.LDLCholesterol


            }
            ).ToList();
            result.TotalCount = list.Count();
            result.TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.UserId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.list = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
